package squareBorderPackage;

public class Door extends Border {

	/**
	 * Initialize this new door with given state (open or closed).
	 * 
	 * @param 	opened
	 *          The initial value for the open state of this new door.
	 * @param	squares
	 * 			
	 */
	public Door(boolean opened, Square[] squares,
			Direction directionOfFirstSquare) {
		super(squares, directionOfFirstSquare);
		this.opened = opened;
	}
	
	public Door(boolean opened, Square square, Direction direction) {
		super(square, direction);
		this.opened = opened;
	}

	/**
	 * Return whether the door is opened or not.
	 */
	public boolean isOpened() {
		return opened;
	}

	/**
	 * Set whether the door is opened or not.
	 * 
	 * @param 	opened
	 *          the boolean referencing whether the door is opened or not
	 */
	public void setOpened(boolean opened) {
		if (opened && !this.opened)
			getSquares()[0].mergeWith(getSquares()[1]);
		this.opened = opened;
	}

	/**
	 * Variable registering whether the door is opened or not.
	 */
	private boolean opened;

	/**
	 * Return whether this wall is slippery or not.
	 */
	public boolean getSlippery() {
		return slippery;
	}

	/**
	 * Variable registering whether the door is slippery or not.
	 */
	private final static boolean slippery = false;

	/**
	 * Method returning whether this Door is an obstacle or not.
	 * 
	 * @return 	... 
	 * 			|result==!opened
	 */
	public boolean isObstacle() {
		if (opened == true)
			return false;
		return true;
	}

	/**
	 * @return 	This border if the other border is not effective 
	 * 			| if (other == null) 
	 * 			| then (result == this)
	 * @return 	The other border if the other border is a door and this door is opened. 
	 * 			|if (other instanceof Door && this.isOpened()) 
	 * 			| then (result == other)
	 * @return 	This border if the other Border is a Door and this door is not opened. 
	 * 			|if (other instanceof Door && ! this.isOpened()) 
	 * 			| then (result == this)
	 * @return 	This border if the other border is a open border. 
	 * 			|if (other instanceof OpenBorder) 
	 * 			| then (result == this)
	 * @return 	The strongest border of the other border and this border in all
	 *         	other cases. 
	 *         	|if (! (other == null || 
	 *         	| other instanceof Door ||
	 *         	| other instanceof OpenBorder)) 
	 *         	| then (result == other.getStrongestBorder(this)
	 */
	@Override
	public Border getStrongestBorder(Border other) {
		if (other == null)
			return this;
		if (other instanceof Door) {
			if (this.isOpened())
				return other;
			return this;
		}
		if (other instanceof OpenBorder)
			return this;
		return other.getStrongestBorder(this);
	}
}
